/*
 * NewGameSettings.java
 *
 * Created on 2011-05-09, 20:34:07
 */
package battleship;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Tomek
 */
@SuppressWarnings("serial")
public class NewGameSettings extends javax.swing.JFrame{
	MainFrame parent;
	Rules rules;
	JTable table;
	KeyAdapter keyAdapter = new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent evt) {
					shipsInGameTableKeyPressed(evt);
      }
    };
	Collection<JComponent> containers = new HashSet<JComponent>();
	/** Creates new form NewGameSettings */
	public NewGameSettings(Rules rul, MainFrame p) {
		super();
		rules = rul;
		parent = p;
		parent.gamePhase = GamePhase.START;
		initComponents();
		packContainers();
		for(JComponent c: containers){
				c.addKeyListener(keyAdapter);
		}
		addKeyListener(keyAdapter);
	}

	private void packContainers() {
		containers.add(finishButton);
		containers.add(addShipButton);
		containers.add(removeShipsButton);
		containers.add(computerCheckBox);
		containers.add(computerStrategyMediumButton);
		containers.add(computerStrategyExtraHardButton);
		containers.add(computerStrategyEasyButton);
		containers.add(playerName1);
		containers.add(playerName2);
		containers.add(corneringAllowedCheckBox);
		containers.add(curvedShipsAllowedCheckBox);
		containers.add(heightSpinner);
		containers.add(widthSpinner);
		containers.add(shipsInGameTable);
		containers.add(shipSizeSpinner);
		containers.add(amountOfShipsSpinner);
	}

	void clear() {
		rules.player1 = new HumanPlayer(rules);
		shipsInGameTable.setModel(new MyTableModel());
		heightSpinner.setValue(new Integer(10));
		widthSpinner.setValue(new Integer(10));
		finishButton.setEnabled(true);
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    computerStrategyButtonGroup = new javax.swing.ButtonGroup();
    player1Label = new javax.swing.JLabel();
    player2Label = new javax.swing.JLabel();
    playerName1 = new javax.swing.JTextField();
    playerName2 = new javax.swing.JTextField();
    computerCheckBox = new javax.swing.JCheckBox();
    finishButton = new javax.swing.JButton();
    jSeparator1 = new javax.swing.JSeparator();
    corneringAllowedCheckBox = new javax.swing.JCheckBox();
    curvedShipsAllowedCheckBox = new javax.swing.JCheckBox();
    jSeparator2 = new javax.swing.JSeparator();
    sizeLabel = new javax.swing.JLabel();
    heightLabel = new javax.swing.JLabel();
    widthLabel = new javax.swing.JLabel();
    heightSpinner =  new javax.swing.JSpinner(new javax.swing.SpinnerNumberModel(10,5,25,1));
    //new javax.swing.SpinnerNumberModel(10, 5, 50, 1);
    widthSpinner = new javax.swing.JSpinner(new javax.swing.SpinnerNumberModel(10,5,25,1));
    jScrollPane1 = new javax.swing.JScrollPane();
    shipsInGameTable = new javax.swing.JTable();
    shipSizeSpinner = new javax.swing.JSpinner(new javax.swing.SpinnerNumberModel(1,1,20,1));
    addShipButton = new javax.swing.JButton();
    sizeOfShipLabel = new javax.swing.JLabel();
    amountOfShipsLabel = new javax.swing.JLabel();
    amountOfShipsSpinner = new javax.swing.JSpinner(new javax.swing.SpinnerNumberModel(1,1,20,1));
    removeShipsButton = new javax.swing.JButton();
    computerStrategyMediumButton = new javax.swing.JRadioButton();
    computerStrategyExtraHardButton = new javax.swing.JRadioButton();
    computerStrategyEasyButton = new javax.swing.JRadioButton();

    //computerStrategyButtonGroup.add(computerStrategyMediumButton);
    //computerStrategyButtonGroup.add(computerStrategyExtraHardButton);
    //computerStrategyButtonGroup.add(computerStrategyEasyButton);

    setTitle("Settings");
    setMinimumSize(new java.awt.Dimension(240, 240));
    setName("New Game Settings"); // NOI18N
    setResizable(false);
    addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        formMouseClicked(evt);
      }
    });

    player1Label.setText("Player 1");

    player2Label.setText("Player 2");

    playerName1.setText("Player1");
    playerName1.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        playerName1FocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        playerName1FocusLost(evt);
      }
    });
    playerName1.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyTyped(java.awt.event.KeyEvent evt) {
        playerName1KeyTyped(evt);
      }
    });

    playerName2.setText("Player2");
    playerName2.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        playerName2FocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        playerName2FocusLost(evt);
      }
    });
    playerName2.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        playerName2KeyPressed(evt);
      }
    });

    computerCheckBox.setText("Computer");
    computerCheckBox.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        computerCheckBoxActionPerformed(evt);
      }
    });

    finishButton.setText("Finish");

    org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, playerName1, org.jdesktop.beansbinding.ObjectProperty.create(), finishButton, org.jdesktop.beansbinding.BeanProperty.create("nextFocusableComponent"));
    bindingGroup.addBinding(binding);

    finishButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        finishButtonActionPerformed(evt);
      }
    });

    corneringAllowedCheckBox.setText("Cornering allowed");

    curvedShipsAllowedCheckBox.setText("Curved ships allowed");

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, sizeLabel, org.jdesktop.beansbinding.ObjectProperty.create(), curvedShipsAllowedCheckBox, org.jdesktop.beansbinding.BeanProperty.create("nextFocusableComponent"));
    bindingGroup.addBinding(binding);

    sizeLabel.setText("Size:");

    heightLabel.setText("Height:");

    widthLabel.setText("Width:");

    shipsInGameTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e){
        if(shipsInGameTable.getSelectedRow()!=-1){
          removeShipsButton.setEnabled(true);
        }else{
          removeShipsButton.setEnabled(false);
        }
      }
    });
    shipsInGameTable.setModel(new MyTableModel());
    shipsInGameTable.getTableHeader().setReorderingAllowed(false);

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, finishButton, org.jdesktop.beansbinding.ObjectProperty.create(), shipsInGameTable, org.jdesktop.beansbinding.BeanProperty.create("nextFocusableComponent"));
    bindingGroup.addBinding(binding);

    shipsInGameTable.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        shipsInGameTableKeyPressed(evt);
      }
    });
    jScrollPane1.setViewportView(shipsInGameTable);
    shipsInGameTable.getColumnModel().getColumn(1).setResizable(false);

    addShipButton.setText("Add Ship");

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, shipsInGameTable, org.jdesktop.beansbinding.ObjectProperty.create(), addShipButton, org.jdesktop.beansbinding.BeanProperty.create("nextFocusableComponent"));
    bindingGroup.addBinding(binding);

    addShipButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        addShipButtonActionPerformed(evt);
      }
    });

    sizeOfShipLabel.setText("Size of ship");

    amountOfShipsLabel.setText("Amount of ships");

    removeShipsButton.setText("Remove ships");
    removeShipsButton.setEnabled(false);
    removeShipsButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        removeShipsButtonActionPerformed(evt);
      }
    });

    computerStrategyButtonGroup.add(computerStrategyMediumButton);
    computerStrategyMediumButton.setSelected(true);
    computerStrategyMediumButton.setText("Medium");
    computerStrategyMediumButton.setEnabled(false);

    computerStrategyButtonGroup.add(computerStrategyExtraHardButton);
    computerStrategyExtraHardButton.setText("Extra Hard");
    computerStrategyExtraHardButton.setEnabled(false);

    computerStrategyButtonGroup.add(computerStrategyEasyButton);
    computerStrategyEasyButton.setText("Easy");
    computerStrategyEasyButton.setEnabled(false);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(30, 30, 30)
            .add(corneringAllowedCheckBox)
            .add(64, 64, 64)
            .add(curvedShipsAllowedCheckBox)
            .addContainerGap(242, Short.MAX_VALUE))
          .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 600, Short.MAX_VALUE)
            .add(finishButton)
            .addContainerGap())
          .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(removeShipsButton)
                .add(46, 46, 46)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(org.jdesktop.layout.GroupLayout.TRAILING, heightLabel)
                  .add(org.jdesktop.layout.GroupLayout.TRAILING, widthLabel))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(layout.createSequentialGroup()
                    .add(27, 27, 27)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                      .add(org.jdesktop.layout.GroupLayout.TRAILING, widthSpinner)
                      .add(org.jdesktop.layout.GroupLayout.TRAILING, heightSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                  .add(layout.createSequentialGroup()
                    .add(18, 18, 18)
                    .add(sizeLabel))))
              .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                  .add(org.jdesktop.layout.GroupLayout.LEADING, shipSizeSpinner)
                  .add(org.jdesktop.layout.GroupLayout.LEADING, sizeOfShipLabel))
                .add(26, 26, 26)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(layout.createSequentialGroup()
                    .add(amountOfShipsSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(32, 32, 32)
                    .add(addShipButton))
                  .add(amountOfShipsLabel))))
            .add(15, 15, 15))))
      .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
      .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
      .add(layout.createSequentialGroup()
        .add(32, 32, 32)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
          .add(layout.createSequentialGroup()
            .add(player1Label)
            .add(18, 18, 18)
            .add(playerName1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 98, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
          .add(layout.createSequentialGroup()
            .add(player2Label)
            .add(18, 18, 18)
            .add(playerName2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)))
        .add(18, 18, 18)
        .add(computerCheckBox)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(computerStrategyExtraHardButton)
          .add(computerStrategyEasyButton)
          .add(computerStrategyMediumButton))
        .addContainerGap(253, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(14, 14, 14)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
            .add(player1Label)
            .add(playerName1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
          .add(computerStrategyEasyButton))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
            .add(player2Label)
            .add(playerName2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(computerCheckBox))
          .add(computerStrategyMediumButton))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(computerStrategyExtraHardButton)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 10, Short.MAX_VALUE)
        .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(corneringAllowedCheckBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(curvedShipsAllowedCheckBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(sizeLabel)
            .add(18, 18, 18)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
              .add(heightLabel)
              .add(heightSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(17, 17, 17)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(widthSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
              .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(widthLabel)
                .add(removeShipsButton))))
          .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .add(7, 7, 7)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(sizeOfShipLabel)
          .add(amountOfShipsLabel))
        .add(2, 2, 2)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(finishButton)
          .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
            .add(shipSizeSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(amountOfShipsSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(addShipButton)))
        .addContainerGap())
    );

    bindingGroup.bind();

    pack();
  }// </editor-fold>//GEN-END:initComponents

		private void finishButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishButtonActionPerformed
			rules.setSize(new Integer(widthSpinner.getValue().toString()),
								new Integer(heightSpinner.getValue().toString())
								);
			rules.setCurvedShipsAloowed(curvedShipsAllowedCheckBox.isSelected());
			int totalShipSize=0, maxShipSize=(Integer)shipsInGameTable.getValueAt(shipsInGameTable.getRowCount()-1, 0);;
			for(int i=shipsInGameTable.getRowCount()-1;i>=0;--i){
				totalShipSize+=(Integer)shipsInGameTable.getValueAt(i, 0)*(Integer)shipsInGameTable.getValueAt(i, 1);
			}
			if(totalShipSize*2.7> rules.xSize*rules.ySize || (maxShipSize>(rules.xSize>rules.ySize?rules.xSize:rules.ySize) && !rules.curvedShipsAllowed)){
				JOptionPane.showMessageDialog(parent, "Too many ships!", "Warning",
				JOptionPane.WARNING_MESSAGE);
			}else{
				rules.setComputerPlayer(computerCheckBox.isSelected());
				if(computerCheckBox.isSelected()){
					if(computerStrategyEasyButton.isSelected()) rules.setComputerStrategy(ComputerStrategy.Easy);
					if(computerStrategyMediumButton.isSelected()) rules.setComputerStrategy(ComputerStrategy.Medium);
					if(computerStrategyExtraHardButton.isSelected()) rules.setComputerStrategy(ComputerStrategy.ExtraHard);
				}
				rules.setCorneringAllowed(corneringAllowedCheckBox.isSelected());
				rules.setPlayerNames(playerName1.getText(),playerName2.getText());
				
				rules.shipsInGame = (MyTableModel) shipsInGameTable.getModel();
				setVisible(false);

				GameSteer.setThings(parent);
				dispose();
				GameSteer.gameCreator();
			}
		}//GEN-LAST:event_finishButtonActionPerformed

		private void addShipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addShipButtonActionPerformed
			finishButton.setEnabled(true);
			Integer sizeOfShip = new Integer(shipSizeSpinner.getValue().toString());
			Integer amountOfShips = new Integer(amountOfShipsSpinner.getValue().toString());
			for(int i=0; i<shipsInGameTable.getRowCount(); ++i){
				if (((Integer)shipsInGameTable.getValueAt(i, 0)).equals(sizeOfShip)){
					shipsInGameTable.getModel().setValueAt((Integer)shipsInGameTable.getValueAt(i, 1)+amountOfShips,i,1);
					return;
				}
				if ((Integer)shipsInGameTable.getValueAt(i, 0)>sizeOfShip){
					((DefaultTableModel)shipsInGameTable.getModel()).insertRow( i,new Integer[]{sizeOfShip, amountOfShips});
					return;
				}
			}
			((DefaultTableModel)shipsInGameTable.getModel()).addRow(new Integer[]{sizeOfShip, amountOfShips});
		}//GEN-LAST:event_addShipButtonActionPerformed

		private void removeShipsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeShipsButtonActionPerformed
		while(shipsInGameTable.getSelectedRow()!=-1)
				((DefaultTableModel)shipsInGameTable.getModel()).removeRow(shipsInGameTable.getSelectedRow());
				removeShipsButton.setEnabled(false);
				if(shipsInGameTable.getRowCount()==0){
					finishButton.setEnabled(false);
				}
		}//GEN-LAST:event_removeShipsButtonActionPerformed

		private void playerName1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_playerName1FocusGained
			if(playerName1.getText().equals("Name1"))
				playerName1.setText("");
		}//GEN-LAST:event_playerName1FocusGained

		private void playerName1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_playerName1FocusLost
			if(playerName1.getText().equals(""))
				playerName1.setText("Name1");
		}//GEN-LAST:event_playerName1FocusLost

		private void playerName2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_playerName2FocusGained
			if(playerName2.getText().equals("Name2"))
					playerName2.setText("");
		}//GEN-LAST:event_playerName2FocusGained

		private void playerName2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_playerName2FocusLost
			if(playerName2.getText().equals(""))
				playerName2.setText("Name2");
		}//GEN-LAST:event_playerName2FocusLost

		private void computerCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_computerCheckBoxActionPerformed
			for(Enumeration<AbstractButton> e = computerStrategyButtonGroup.getElements(); e.hasMoreElements();)
				e.nextElement().setEnabled(computerCheckBox.isSelected());
		}//GEN-LAST:event_computerCheckBoxActionPerformed

		private void shipsInGameTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_shipsInGameTableKeyPressed
			switch(evt.getKeyCode()){
				case KeyEvent.VK_DELETE:{
					removeShipsButtonActionPerformed(null);
					break;
				}
				case KeyEvent.VK_ESCAPE:{
					setVisible(false);
					break;
				}
			}
		}//GEN-LAST:event_shipsInGameTableKeyPressed

		private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
			requestFocus();
		}//GEN-LAST:event_formMouseClicked

		private void playerName1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_playerName1KeyTyped
			if(playerName1.getText().length()>=20)
				try {
					playerName1.setText(playerName1.getText(0, 20));
				} catch (BadLocationException ex) {
				}
		}//GEN-LAST:event_playerName1KeyTyped

		private void playerName2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_playerName2KeyPressed
			if(playerName2.getText().length()>=20)
				try {
					playerName2.setText(playerName2.getText(0, 20));
				} catch (BadLocationException ex) {
				}
		}//GEN-LAST:event_playerName2KeyPressed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton addShipButton;
  private javax.swing.JLabel amountOfShipsLabel;
  private javax.swing.JSpinner amountOfShipsSpinner;
  private javax.swing.JCheckBox computerCheckBox;
  private javax.swing.ButtonGroup computerStrategyButtonGroup;
  private javax.swing.JRadioButton computerStrategyEasyButton;
  private javax.swing.JRadioButton computerStrategyExtraHardButton;
  private javax.swing.JRadioButton computerStrategyMediumButton;
  private javax.swing.JCheckBox corneringAllowedCheckBox;
  private javax.swing.JCheckBox curvedShipsAllowedCheckBox;
  private javax.swing.JButton finishButton;
  private javax.swing.JLabel heightLabel;
  private javax.swing.JSpinner heightSpinner;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JSeparator jSeparator1;
  private javax.swing.JSeparator jSeparator2;
  private javax.swing.JLabel player1Label;
  private javax.swing.JLabel player2Label;
  private javax.swing.JTextField playerName1;
  private javax.swing.JTextField playerName2;
  protected javax.swing.JButton removeShipsButton;
  private javax.swing.JSpinner shipSizeSpinner;
  protected javax.swing.JTable shipsInGameTable;
  private javax.swing.JLabel sizeLabel;
  private javax.swing.JLabel sizeOfShipLabel;
  private javax.swing.JLabel widthLabel;
  private javax.swing.JSpinner widthSpinner;
  private org.jdesktop.beansbinding.BindingGroup bindingGroup;
  // End of variables declaration//GEN-END:variables
}
